{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 阶乘\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "764c95c6-476c-4c40-bdf6-e458c704016f",
   "metadata": {},
   "source": [
    "该代码用于计算用户输入的整数的阶乘，适用于正整数和零的阶乘计算，并对负数输入进行处理。阶乘（Factorial）是指从$1$开始到该整数的所有正整数的乘积，记作$n!$。其定义如下：\n",
    "\n",
    "- 如果$n < 0$，则阶乘不存在。\n",
    "- 如果$n = 0$，则$0! = 1$（这是一个数学定义）。\n",
    "- 如果$n > 0$，则阶乘的计算公式为：\n",
    "\n",
    "  $$\n",
    "  n! = 1 \\times 2 \\times 3 \\times \\dots \\times n\n",
    "  $$\n",
    "\n",
    "### 代码流程\n",
    "\n",
    "1. **输入处理与初始化**：代码首先从用户获取一个整数输入，并将阶乘结果变量`factorial`初始化为$1$。\n",
    "   \n",
    "2. **条件判断**： \n",
    "   - 如果输入的是负数，代码输出“负数没有阶乘”。\n",
    "   - 如果输入的是零，则根据定义直接输出结果为$1$。\n",
    "   - 如果输入的是正整数$n$，代码使用`for`循环计算阶乘，从$1$到$n$依次相乘，得到$n!$的值。\n",
    "\n",
    "3. **输出结果**：代码最终将计算得到的阶乘值显示给用户。\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码通过条件判断和循环结构完成了阶乘计算。阶乘在数学中用于排列、组合及概率计算，因此具有广泛的应用价值。代码设计考虑了不同输入情况，确保了程序的健壮性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b027fe46-03ae-4f39-8561-02e12041fd9d",
   "metadata": {},
   "source": [
    "## 计算阶乘的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e20acf86-fd29-4a09-a6e3-8d74e5cf48d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter an integer:  10\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"Enter an integer: \"))  # 输入一个整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "add8647a-e04a-4b15-b1af-7d89f7615a9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "factorial = 1  # 初始化阶乘结果为1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "535e4b0c-1caf-4953-9e3d-b9169bab77ae",
   "metadata": {},
   "source": [
    "## 检查输入的整数是负数、零还是正数并计算阶乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "037858fb-e07e-4345-a44a-8c6ec21bf5aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The factorial of 10 is 3628800\n"
     ]
    }
   ],
   "source": [
    "if num < 0:  # 如果输入的是负数\n",
    "    print(\"Factorial does not exist for negative numbers\")  # 输出负数没有阶乘\n",
    "elif num == 0:  # 如果输入的是0\n",
    "    print(\"The factorial of 0 is \", factorial)  # 输出0的阶乘为1\n",
    "else:  # 如果输入的是正数\n",
    "    for i in range(1, num + 1):  # 循环计算阶乘\n",
    "       factorial = factorial * i  # 累乘计算阶乘\n",
    "    print(\"The factorial of\", num, \"is\", factorial)  # 输出正整数的阶乘结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
